#######################################################################################
### R file to do some counterfactual analysis for the three illustrations in Laron K Williams, "Compression, Temporal Dependence, and the Sensitivity of Quantities of Interest"
###
### Created: 8-29-17
### Modified: 3-6-18
###
#######################################################################################

library(foreign)
library(ggplot2)
library(lattice)
library(fields)
library(separationplot)
library(scales)
library(gridExtra)
library("WhatIf")

# Set working directory
#setwd("")

#######################################################################################
### Way and Weeks (2014): Personalistic Regimes and Nuclear Proliferation
###
### 4 counterfactual scenarios (all others at mean)
### 1) t = mean, t^2 = mean, t^3 = mean (incorrect) 
### 2) t = mean, t^2 = mean^2, t^3 = mean^3 (correct)
### 3) t = median...
### 4) t = mode...
#######################################################################################

### Load the master data
ww = read.dta("Way and Weeks 2014/Data/Counterfactuals/WW.dta", convert.underscore = TRUE)

### Load the four counterfactual scenarios
ww.cf = read.dta("Way and Weeks 2014/Data/Counterfactuals/WW_CF.dta", convert.underscore = TRUE)

### Counterfactual analysis
wwcf.result <- whatif(data = ww, cfact = ww.cf, mc.cores = 1)

### Convex hull test (FALSE = extrapolation)
wwcf.result$in.hull

### Geometric variability (GV) of the observed data: this is roughly the average distance among all pairs of observations in the data; counterfactuals that are within one GV are "sufficiently close to the counterfactual to make reasonable inferences" (King and Zeng 2006: 138).
wwcf.result$geom.var

### Proportion of data points nearby (i.e., within 1 GV) of the counterfactual
wwcf.result$sum.stat


#######################################################################################
### Cunningham (2013): Opposition Factions and Civil War Onset
###
### 4 counterfactual scenarios (all others at mean)
### 1) t = mean, mean of 3 splines (incorrect)
### 2) t = mean, 3 splines based on mean (correct)
### 3) t = median, 3 splines based on median
### 4) t = mode, 3 splines based on mode
### 5) Replication scenario
#######################################################################################

### Load the master data
c = read.dta("Cunningham 2013/Data/Counterfactuals/C.dta", convert.underscore = TRUE)

### Load the four counterfactual scenarios
c.cf = read.dta("Cunningham 2013/Data/Counterfactuals/C_CF.dta", convert.underscore = TRUE)

### Counterfactual analysis
ccf.result <- whatif(data = c, cfact = c.cf, mc.cores = 1)

### Convex hull test (FALSE = extrapolation)
ccf.result$in.hull

### Geometric variability (GV) of the observed data: this is roughly the average distance among all pairs of observations in the data; counterfactuals that are within one GV are "sufficiently close to the counterfactual to make reasonable inferences" (King and Zeng 2006: 138).
ccf.result$geom.var



#######################################################################################
### Flores-Macias and Kreps (2013): Partisan Disposition and Implementation of War Taxes
###
### 4 counterfactual scenarios (all others at mean)
### 1) t = mean, mean of 3 splines (incorrect)
### 2) t = mean, 3 splines based on mean (correct)
### 3) t = median, 3 splines based on median
### 4) t = mode, 3 splines based on mode
### 5) Replication scenario
#######################################################################################

### Load the master data
fmk = read.dta("Flores-Macias and Kreps 2013/Data/Counterfactuals/FMK.dta", convert.underscore = TRUE)

### Load the four counterfactual scenarios
fmk.cf = read.dta("Flores-Macias and Kreps 2013/Data/Counterfactuals/FMK_CF.dta", convert.underscore = TRUE)

### Counterfactual analysis
fmkcf.result <- whatif(data = fmk, cfact = fmk.cf, mc.cores = 1)

### Convex hull test (FALSE = extrapolation)
fmkcf.result$in.hull

### Geometric variability (GV) of the observed data: this is roughly the average distance among all pairs of observations in the data; counterfactuals that are within one GV are "sufficiently close to the counterfactual to make reasonable inferences" (King and Zeng 2006: 138).
fmkcf.result$geom.var

### Proportion of data points nearby (i.e., within 1 GV) of the counterfactual
fmkcf.result$sum.stat
